$(function () {

    $('#side-menu ul').eq(0).addClass('in');

    // $('#btn_search').on('click',function(){
    //     selectUserByPhone();
    // })
    var phone = getUrlParam('phone');
    if (phone != null && phone != ''){
        selectUserByPhone(phone);
    }

    $(document).keydown(function(event){
        if(event.keyCode==13){
            $("#btn_search").click();
        }
    });
});


/**
 *  功能描述：获取专线列表
 *  请求方式：GET
 *  请求地址：/api/line_user/list
 *  函数名称：getZUsersList
 */
function selectUserByPhone(phone) {
    $.ajax({
        url: whole_path+'/api/line_user/selectByPhone?phoneNumber='+phone,
        type: 'GET',
        dataType: 'json',
        cache:false,
        // data: $('#phone_number_form').serialize(),
        beforeSend:function(data){
            $.progressBar({message:'<p>正在努力加载数据...</p>',modal:true,canCancel:true});
        },
        success:function(data){
            if(data.status == 0) {
                var json = data.data;
                var c_user = json.cUserResponse;
                var z_user = json.zUserResponse;
                var temp = "";
                var operation_c ,operation_z= '';
                if (c_user != null){
                    operation_c = '<a class="btn-sm btn-info" href="../cuser/cuser_edit.jsp?id='+c_user.id+'"  title="编辑"><i class="fa fa-pencil-square-o"></i></a>&nbsp;'
                        +'<a href="../cuser/cuser_detail.jsp?id='+c_user.id+'&account='+c_user.userName+'" class="btn-sm blue-bg" title="详情"><i class="fa fa-list-alt"></i></a>&nbsp;'
                        +'<a class="btn-sm btn-warning" data-toggle="modal" data-target="#unbindModal" title="解绑"  onclick="UserUnbind('+c_user.id+',1,\''+encodeURIComponent(c_user.userName)+'\')"><i class="fa fa-magnet"></i></a>&nbsp;'
                        +'<a class="btn-sm btn-danger" data-toggle="modal" data-target="#modifyTimeModal" title="修改到期时间" onclick="getUserTime('+c_user.id+',1,\''+encodeURIComponent(c_user.userName)+'\')"><i class="fa fa-calendar"></i></a>';

                    temp += '<tr>'
                        + '<td data-title="帐号">' + c_user.userName + '</td>'
                        + '<td data-title="注册时间">' + timeFormat(c_user.registerTime) + '</td>'
                        + '<td data-title="用户姓名">' + c_user.realName + '</td>'
                        + '<td data-title="联系电话">' + c_user.phoneNumber + '</td>'
                        + '<td data-title="开户人">' + (c_user.operateName == null?'':c_user.operateName) + '</td>'
                        + '<td data-title="用户类型">' + c_user.userType + '</td>'
                        + '<td data-title="操作">' + operation_c + '</td>'
                        + '</tr>';

                }
                if (z_user != null){
                    operation_z = ' <a class="btn-sm btn-info" href="zuser_edit.jsp?id='+z_user.id+'"  title="编辑"><i class="fa fa-pencil-square-o"></i></a>&nbsp;'
                    +'<a href="zuser_detail.jsp?id='+z_user.id+'&account='+z_user.userName+'" class="btn-sm blue-bg" title="详情"><i class="fa fa-list-alt"></i></a>&nbsp;'
                    +'<a class="btn-sm btn-warning" data-toggle="modal" data-target="#unbindModal" title="解绑"  onclick="UserUnbind('+z_user.id+',2,\''+encodeURIComponent(z_user.userName)+'\')"><i class="fa fa-magnet"></i></a>&nbsp;'
                    +'<a class="btn-sm btn-danger" data-toggle="modal" data-target="#modifyTimeModal" title="修改到期时间" onclick="getUserTime('+z_user.id+',2,\''+encodeURIComponent(z_user.userName)+'\')"><i class="fa fa-calendar"></i></a>&nbsp;'
                    +'<a class="btn-sm navy-bg" href="../cuser/cars_team_list.jsp?id='+z_user.id+'" title="我的车队" ><i class="fa fa-truck"></i></a>';

                    temp += '<tr>'
                        + '<td data-title="帐号">' + z_user.userName + '</td>'
                        + '<td data-title="注册时间">' + timeFormat(z_user.registerTime) + '</td>'
                        + '<td data-title="用户姓名">' + z_user.realName + '</td>'
                        + '<td data-title="联系电话">' + z_user.phoneNumber + '</td>'
                        + '<td data-title="开户人">' + (z_user.operateName == null?'':z_user.operateName) + '</td>'
                        + '<td data-title="用户类型">' + z_user.userType + '</td>'
                        + '<td data-title="操作">' + operation_z + '</td>'
                        + '</tr>';

                }
                $('#phone_number_list tbody').html(temp);
            }
        },
        complete:function(){
            $.progressBar().close();
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            $.toast('服务器未响应,请稍候重试',5000);
        }
    })
}



/**
 *  功能描述：获取解绑数据列表
 *  请求方式：GET
 *  请求地址：/api/limit/list
 *  函数名称：userUnbind
 */
function UserUnbind(id,type,userName) {
    $.ajax({
        url: whole_path+'/api/limit/list',
        type: 'GET',
        dataType: 'json',
        cache:false,
        data: {
            userId:id,
            userType:type
        },
        beforeSend:function(data){
            $.progressBar({message:'<p>正在努力加载数据...</p>',modal:true,canCancel:true});
        },
        success:function(data){
            if(data.status == 0) {
                var json = data.data;
                var list = json;
                var temp = "";
                if (list != null && list.length>0){
                    var operation = ''; //操作按钮
                    $.each(list, function (index, item) {
                        var loginType = item.platformType;
                        switch (loginType){
                            case 0:
                                loginType = "PC"
                                break;
                            case 1:
                                loginType = "iOS"
                                break;
                            case 2:
                                loginType = "Android"
                                break;
                        }
                        if (item.hardware != null && item.hardware != ''){
                            operation = '<a class="btn-sm btn-warning btn-rounded" onclick="userUnbindCommit('+item.id+','+type+',\''+encodeURIComponent(userName)+'\');">解绑</a>';
                        }else {
                            operation = '<a class="btn-sm btn-info btn-rounded" >已解绑</a>';
                        }
                        temp += '<tr class="gradeA">'
                            + '<td data-title="登录类型">' + loginType + '</td>'
                            + '<td data-title="操作">' + operation + '</td>'
                            + '</tr>';
                    })
                    $('#unbind_list tbody').html(temp);
                }else{
                    $.toast("暂无解绑的数据",3000);
                    $('#c_user_unbind_list tbody').html('');
                }
            }
        },
        complete:function(){
            $.progressBar().close();
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            $.toast('服务器未响应,请稍候重试',5000);
        }
    })
}



/**
 *  功能描述：解绑
 *  请求方式：POST
 *  请求地址：/api/limit/update
 *  函数名称：modifyUnbind
 */

function userUnbindCommit(id,type,userName){
    $('#unbindModal').modal('hide');
    $.ajax({
        url:whole_path+ '/api/limit/update',
        type: 'POST',
        dataType: 'json',
        data: {
            'id':id,
            'isUnbind':1,
            'userName':userName,
            'userType':type
        },
        beforeSend:function(){
            $.progressBar({message:'<p>正在努力加载数据...</p>',modal:false,canCancel:true});
        },
        success:function(data){
            if(data.status == 0){
                $.toast('操作成功',3000);
                selectUserByPhone();
            }else{
                $.toast(data.msg,3000);
            }

        },
        complete:function(){
            $.progressBar().close();
        },
        error:function(XMLHttpRequest,textStatus,errorThrown){
            $.toast('服务器未响应,请稍候重试',5000);
        }
    })

}




/**
 *  功能描述：获取到期时间列表
 *  请求方式：GET
 *  请求地址：/api/limit/list
 *  函数名称：reviseCUserTime
 */
function getUserTime(id,type,userName) {
    $.ajax({
        url: whole_path+'/api/limit/list',
        type: 'GET',
        dataType: 'json',
        cache:false,
        data: {
            userId:id,
            userType:type
        },
        beforeSend:function(data){
            $.progressBar({message:'<p>正在努力加载数据...</p>',modal:true,canCancel:true});
        },
        success:function(data){
            if(data.status == 0) {
                var json = data.data;
                var list = json;
                var temp = "";
                if (list != null && list.length>0){
                    var operation = ''; //操作按钮
                    var enabled = '';
                    $.each(list, function (index, item) {
                        operation = '<a class="btn-sm btn-danger btn-rounded" onclick="userDateCommit('+item.id+','+index+','+type+',\''+encodeURIComponent(userName)+'\');">提交</a>';

                        if(item.beUsed == 1){
                            enabled = '<input type="radio" name="beUsed'+index+'" value="1" checked>是<input type="radio" name="beUsed'+index+'" value="0">否';
                        }else{
                            enabled = '<input type="radio" name="beUsed'+index+'" value="1">是<input type="radio" name="beUsed'+index+'" value="0" checked>否';
                        }

                        var loginType = item.platformType;
                        switch (loginType){
                            case 0:
                                loginType = "PC"
                                break;
                            case 1:
                                loginType = "iOS"
                                break;
                            case 2:
                                loginType = "Android"
                                break;
                        }

                        temp += '<tr class="gradeA">'
                            + '<td data-title="登录类型">' + loginType + '</td>'
                            + '<td data-title="是否开通">'+enabled+'</td>'
                            + '<td data-title="到期时间"><input id="expireTime'+index+'" name="expireTime" class="laydate-icon form-control layer-date" onclick="laydate()" value="'+timeFormat(item.expireTime)+'"></td>'
                            + '<td data-title="操作">' + operation + '</td>'
                            + '</tr>';
                    })
                    $('#expire_time tbody').html(temp);
                }else{
                    $.toast("没有查到数据",3000);
                    $('#expire_time tbody').html('');
                }
            }
        },
        complete:function(){
            $.progressBar().close();
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            $.toast('服务器未响应,请稍候重试',5000);
        }
    })
}

/**
 *  功能描述：提交修改时间
 *  请求方式：POST
 *  请求地址：/api/limit/update
 *  函数名称：submitCuserDate
 */
function userDateCommit(id,index,type,userName){
    $('#modifyTimeModal').modal('hide');
    var beUsed = $("input[name='beUsed"+index+"']:checked").val();
    var expireTime = $("#expireTime"+index).val();
    $.ajax({
        url:whole_path+ '/api/limit/update',
        type: 'POST',
        dataType: 'json',
        data: {
            'id':id,
            'beUsed':beUsed,
            'expireTime':expireTime,
            'isUnbind':0,
            'userName':userName,
            'userType':type
        },
        beforeSend:function(){
            $.progressBar({message:'<p>正在努力加载数据...</p>',modal:false,canCancel:true});
        },
        success:function(data){
            if(data.status == 0){
                $.toast('操作成功',3000);
                selectUserByPhone();
            }else{
                $.toast(data.msg,3000);
            }

        },
        complete:function(){
            $.progressBar().close();
        },
        error:function(XMLHttpRequest,textStatus,errorThrown){
            $.toast('服务器未响应,请稍候重试',5000);
        }
    })

}